﻿namespace CKFinder.Connector.CommandHandlers
{
    using CKFinder;
    using CKFinder.Connector;
    using System;
    using System.IO;
    using System.Xml;

    internal class GetFoldersCommandHandler : XmlCommandHandlerBase
    {
        protected override void BuildXml()
        {
            if (!base.CurrentFolder.CheckAcl(AccessControlRules.FolderView))
            {
                ConnectorException.Throw(0x67);
            }
            DirectoryInfo info = new DirectoryInfo(base.CurrentFolder.ServerPath);
            if (!info.Exists)
            {
                ConnectorException.Throw(0x74);
            }
            else
            {
                XmlNode node = XmlUtil.AppendElement(base.ConnectorNode, "Folders");
                DirectoryInfo[] directories = info.GetDirectories();
                for (int i = 0; i < directories.Length; i++)
                {
                    string name = directories[i].Name;
                    if (!Config.Current.CheckIsHiddenFolder(name))
                    {
                        int computedMask = Config.Current.AccessControl.GetComputedMask(base.CurrentFolder.ResourceTypeName, base.CurrentFolder.ClientPath + name + "/");
                        if ((computedMask & 1) == 1)
                        {
                            XmlNode node2 = XmlUtil.AppendElement(node, "Folder");
                            XmlUtil.SetAttribute(node2, "name", name);
                            try
                            {
                                XmlUtil.SetAttribute(node2, "hasChildren", (directories[i].GetDirectories().Length > 0) ? "true" : "false");
                            }
                            catch
                            {
                                XmlUtil.SetAttribute(node2, "hasChildren", "true");
                            }
                            XmlUtil.SetAttribute(node2, "acl", computedMask.ToString());
                        }
                    }
                }
            }
        }
    }
}

